home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 06 General Architectures / 06 Rabin / statemch.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-12-10  |  2.8 KB  |  89 lines

  1. /* Copyright (C) Steve Rabin, 2001. 
  2.  * All rights reserved worldwide.
  3.  *
  4.  * This software is provided "as is" without express or implied
  5.  * warranties. You may freely copy and compile this source into
  6.  * applications you distribute provided that the copyright text
  7.  * below is included in the resulting source code, for example:
  8.  * "Portions Copyright (C) Steve Rabin, 2001"
  9.  */
  10.  
  11. #ifndef __STATEMCH_H__
  12. #define __STATEMCH_H__
  13.  
  14. #include <assert.h>
  15. #include "msg.h"
  16. #include "gameobject.h"
  17. #include "debuglog.h"
  18. #include "time.h"
  19. #include "global.h"
  20.  
  21.  
  22. //State Machine Language Macros (put these keywords in the file USERTYPE.DAT in the same directory as MSDEV.EXE)
  23. #define BeginStateMachine        if( state < 0 ) { char statename[64] = "STATE_Global"; if(0) {
  24. #define EndStateMachine            return( true ); } } else { assert( 0 && "Invalid State" ); return( false ); } return( false );
  25. #define State(a)                return( true ); } } else if( a == state ) { char statename[64] = #a; if(0) { 
  26. #define OnMsg(a)                return( true ); } else if( EVENT_Message == event && msg && a == msg->GetMsgName() ) { g_debuglog.LogStateMachineEvent( m_Owner->GetID(), msg, statename, #a, true );
  27. #define OnEvent(a)                return( true ); } else if( a == event ) { g_debuglog.LogStateMachineEvent( m_Owner->GetID(), msg, statename, #a, true );
  28. #define OnUpdate                OnEvent( EVENT_Update )
  29. #define OnEnter                    OnEvent( EVENT_Enter )
  30. #define OnExit                    OnEvent( EVENT_Exit )
  31.  
  32.  
  33.  
  34. enum StateMachineEvent { EVENT_INVALID,
  35.                          EVENT_Update,
  36.                          EVENT_Message,
  37.                          EVENT_Enter,
  38.                          EVENT_Exit
  39. };
  40.  
  41. typedef enum { NO_MSG_SCOPING,
  42.                SCOPE_TO_THIS_STATE
  43. } MSG_Scope;
  44.  
  45.  
  46. class StateMachine
  47. {
  48. public:
  49.  
  50.     StateMachine( GameObject * object );
  51.     ~StateMachine( void ) {}
  52.  
  53.     void Initialize( void );
  54.     void Update( void );
  55.     void SetState( unsigned int newState );
  56.  
  57.     void SendMsg( MSG_Name name, objectID receiver );
  58.     void SendDelayedMsg( float delay, MSG_Name name, objectID receiver );
  59.     void SendDelayedMsgToMe( float delay, MSG_Name name, MSG_Scope scope );
  60.  
  61.     int GetState( void )                        { return( m_currentState ); }
  62.     float GetTimeInState( void )                { return( g_time.GetCurTime() - m_timeOnEnter ); }                
  63.  
  64.     void SetCCReceiver( objectID id )            { m_ccMessagesToGameObject = id; }
  65.     void ClearCCReceiver( void )                { m_ccMessagesToGameObject = 0; }
  66.     objectID GetCCReceiver( void )                { return( m_ccMessagesToGameObject ); }
  67.  
  68.     void Process( StateMachineEvent event, MSG_Object * msg );
  69.  
  70.  
  71. protected:
  72.  
  73.     GameObject * m_Owner;
  74.  
  75.  
  76. private:
  77.  
  78.     unsigned int m_currentState;
  79.     unsigned int m_nextState;
  80.     bool m_stateChange;
  81.     float m_timeOnEnter;
  82.     objectID m_ccMessagesToGameObject;
  83.  
  84.     virtual bool States( StateMachineEvent event, MSG_Object * msg, int state ) = 0;
  85.  
  86. };
  87.  
  88.  
  89. #endif    // __STATEMCH_H__